package my

import akka.actor.{Actor, ActorLogging}
import akka.cluster.ClusterEvent._
import akka.cluster.{Cluster, MemberStatus}

/**
  * Created by shichen on 2017/8/3.
  */
class MyClusterDomainEventEventListener extends Actor
  with ActorLogging {
  Cluster(context.system).subscribe(self,classOf[ClusterDomainEvent])
  override def receive: Receive = {
    case MemberUp(m) =>
      log.info(s"$m up")
    case MemberExited(m) =>
      log.info(s"$m exited")
    case MemberRemoved(member, previousState)=>
      if(previousState == MemberStatus.Exiting) {
        log.info(s"Member $member Previously gracefully exited, REMOVED.")
      } else {
        log.info(s"$member Previously downed after unreachable, REMOVED.")
      }
    case UnreachableMember(member) =>
      log.info(s"$member UNREACHABLE")
    case ReachableMember(member) =>
      log.info(s"$member REACHABLE")
    case state: CurrentClusterState => //Current snapshot state of the cluster. Sent to new subscriber
      log.info(s"Current state of the cluster: $state")
  }

  override def postStop(): Unit = {
    Cluster(context.system).unsubscribe(self)
    super.postStop()
  }
}
